package com.ctz.commonframe.utils;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

/**
 * Created by 陈堂正 on 2016/12/12.
 */
public class NotificationUtil {
    public static void showNotifiction(Context context, Class clazz, int smallIconRes, boolean soundEnabled, String ticker, String title, String content, String tag, int id) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setAutoCancel(true);//点击后消失
        if (smallIconRes > 0)
            builder.setSmallIcon(smallIconRes);//设置通知栏消息标题的头像
        if (soundEnabled)
            builder.setDefaults(NotificationCompat.DEFAULT_SOUND);//设置通知铃声
        if(!TextUtils.isEmpty(ticker))
            builder.setTicker(ticker);
        if(!TextUtils.isEmpty(title))
            builder.setContentTitle(title);
        if(!TextUtils.isEmpty(content))
            builder.setContentText(content);

        if (clazz != null) {
            Intent intent = new Intent(context, clazz);
            PendingIntent intentPend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.setContentIntent(intentPend);
        }
        NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        if(!TextUtils.isEmpty(tag))
            manager.notify(tag, id, builder.build());
        else
            manager.notify(0, builder.build());
    }

    public static void showNotifiction(Context context, Class clazz, int smallIconRes, boolean soundEnabled, String ticker, String title, String content) {
        showNotifiction(context, clazz, smallIconRes, soundEnabled, ticker, title, content);
    }

    public static void cancelNotification(Context context, String tag, int id){
        NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        if(!TextUtils.isEmpty(tag))
            manager.cancel(tag, id);
        else
            manager.cancel(id);
    }

}
